package com.yang.xiaohoshuauth.run;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yang.xiaohoshuauth.domain.dataobject.PermissionDO;
import com.yang.xiaohoshuauth.domain.dataobject.RoleDO;
import com.yang.xiaohoshuauth.domain.dataobject.RolePermissionDO;
import com.yang.xiaohoshuauth.domain.mapper.PermissionDOMapper;
import com.yang.xiaohoshuauth.domain.mapper.RoleDOMapper;
import com.yang.xiaohoshuauth.domain.mapper.RolePermissionDOMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 推送角色权限数据到 Redis 中
 */
@Component
@Slf4j
public class PushRolePermissions2RedisRunner implements ApplicationRunner {


    @Resource
    private RoleDOMapper roleDOMapper;

    @Resource
    private RolePermissionDOMapper rolePermissionDOMapper;

    @Resource
    private PermissionDOMapper permissionDOMapper;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("==> 服务启动，开始同步角色权限数据到 Redis 中...");


        try {
            // todo
            // 查出所有的角色，t_tole表
            List<RoleDO> roleDOS = roleDOMapper.selectEnableList();

            // 判空
            if(CollectionUtil.isEmpty(roleDOS)){
                // 拿到所有角色的 ID
                List<Long> roleIds = roleDOS.stream().map(RoleDO::getId).toList();
                // 查询角色-权限ID关系，t_role_permission_rel
                List<RolePermissionDO> rolePermissionDOS = rolePermissionDOMapper.selectByRoleIds(roleIds);
                // 按角色 ID 分组, 每个角色 ID 对应多个权限 ID
                Map<Long, List<Long>> roleIdPermissionIdsMap = rolePermissionDOS.stream().collect(
                        Collectors.groupingBy(RolePermissionDO::getRoleId,
                                Collectors.mapping(RolePermissionDO::getPermissionId, Collectors.toList()))
                );

                // 查询 APP 端所有被启用的权限
                List<PermissionDO> permissionDOS = permissionDOMapper.selectAppEnabledList();
                // 权限 ID - 权限 DO
                Map<Long, PermissionDO> permissionIdDOMap = permissionDOS.stream().collect(
                        Collectors.toMap(PermissionDO::getId, permissionDO -> permissionDO)
                );
                // 组织 角色ID - 权限集合关系
                Map<Long, List<PermissionDO>> roleIdPermissionDOMap = Maps.newHashMap();
                roleDOS.forEach(roleDO -> {
                    // 当前角色的ID
                    Long roleId = roleDO.getId();
                    // 当前角色 ID 对应的权限 ID 集合
                    List<Long> permissionIds = roleIdPermissionIdsMap.get(roleId);
                    if (CollUtil.isNotEmpty(permissionIds)) {
                        List<PermissionDO> perDOS = Lists.newArrayList();
                        permissionIds.forEach(permissionId -> {
                            // 根据权限 ID 获取具体的权限 DO 对象
                            PermissionDO permissionDO = permissionIdDOMap.get(permissionId);
                            if (Objects.nonNull(permissionDO)) {
                                perDOS.add(permissionDO);
                            }
                        });
                        roleIdPermissionDOMap.put(roleId, perDOS);
                    }

                });
                // 同步至Redis
            }



        } catch (Exception e) {
            log.error("==> 同步角色权限数据到 Redis 中失败: ", e);
        }

        log.info("==> 服务启动，成功同步角色权限数据到 Redis 中...");
    }
}
